/**
 * 
 */
package info.niwota.start.media;

import info.niwota.start.Debug;
import info.niwota.start.Files;
import info.niwota.start.Intents;
import info.niwota.start.KeyguardUtil;
import info.niwota.start.Utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

/**
 * @author qiangli
 *
 */
public class QuickShootActivity extends Activity {

	private static final int RC_RECORD_VIDEO = 11;
	private static final int RC_TAKE_PICTURE = 12;
	
	private static final String SAVED_TEMPFILE = "tempfile";
	
	private static final String TAG = "QuickShootActivity";
	
	private static final String EXT_JPG = ".jpg";
	
	private static final String IMAGE_PREFIX = "photo-";
	
	//private static final String VIDEO_PREFIX = "video-";
	
	private File tempFile;

	protected void doFinish(String path) {
		
		try {
			if (path == null) {
				setResult(RESULT_CANCELED);
			} else {
				Intent i = new Intent();
				i.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
				//
				if (Debug.DEBUG) {
					ComponentName ca = getCallingActivity();
					Log.d(TAG, "returning: " + ca  +" path: " + path);
				}
				setResult(RESULT_OK, i);
			}
		} catch (Exception e) {
			setResult(RESULT_CANCELED);
			
			//
			e.printStackTrace();
		}
		
		quit();
	}

	protected void quit() {
		finish();
		
		Intent i = new Intent(Intents.ACTION_POWERUP_QUIT);
		sendOrderedBroadcast(i, null);

		KeyguardUtil.enableGuard();
	}

	protected String makePrefix(String prefix) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		return prefix+df.format(new Date());
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onActivityResult resultCode " + resultCode + " data: " + data);
		}
		setResult(resultCode);
		String path = null;
		if (resultCode == RESULT_CANCELED) {
			if (tempFile != null) {
				tempFile.delete();
			}
		} else if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case RC_TAKE_PICTURE: {
				path = tempFile.getAbsolutePath();
				break;
			}
			case RC_RECORD_VIDEO: {
				Uri uri = (data == null ? null : data.getData());
				if (uri != null) {
					path = Utils.getDataPath(this, uri);
				}
				break;
			}
			default:
			}
		} 
		try {
			finishActivity(requestCode);
		} catch (Exception e) {
			e.printStackTrace();
		}

		doFinish(path);
	}

//	private void startCamcorderActivity() {
//		try {
//			Intent i = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//			tempFile = Files.getTempFile(VIDEO_PREFIX, ".mpeg");
//			Uri uri = Uri.fromFile(tempFile);
//			i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//			i.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
//			//
//			startActivityForResult(i, RC_RECORD_VIDEO);
//		} catch (Exception e) {
//			e.printStackTrace();
//			// toast();
//			setResult(RESULT_CANCELED);
//			finish();
//		}
//	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		if (Debug.DEBUG) {
			Log.d(TAG, "onCreate bundle: " + savedInstanceState);
		}
		
		if (savedInstanceState != null) {
			tempFile = (File) savedInstanceState.getSerializable(SAVED_TEMPFILE);
			
			if (Debug.DEBUG) {
				Log.d(TAG, "onCreate restored: " + tempFile);
			}
		}
		
		KeyguardUtil.disableGuard(this);
	}
	

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onSaveInstanceState file outState: " + outState);
		}
		outState.putSerializable(SAVED_TEMPFILE, tempFile);
		//
		super.onSaveInstanceState(outState);
	}

	protected void startCamcorderActivity() {
		try {
			Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
			intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
			startActivityForResult(intent, RC_RECORD_VIDEO);
		} catch (Exception e) {
			e.printStackTrace();
			
			toast(e.getMessage()+"");
			setResult(RESULT_CANCELED);
			
			quit();
		}
	}
	
	protected void startCameraActivity() {
		try {
			Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);	
			
			String prefix = makePrefix(IMAGE_PREFIX);
			File sub = Files.getImageFolder(this);
			tempFile = Files.getTempFile(sub, prefix, EXT_JPG);
			Uri uri = Uri.fromFile(tempFile);
			//
			if (Debug.DEBUG) {
				Log.d(TAG, "startCameraActivity uri: " + uri);
			}
			//
			i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
			//
			startActivityForResult(i, RC_TAKE_PICTURE);
		} catch (Exception e) {
			e.printStackTrace();
			
			toast(e.getMessage()+"");
			setResult(RESULT_CANCELED);
			
			quit();
		}
	}

	private void toast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
	
	protected void setSecureFullscreen() {
		final Window w = getWindow();
		w.requestFeature(Window.FEATURE_NO_TITLE);
		w.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		w.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		w.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
	}
}
